package com.aaa.controller;

import com.aaa.entity.ResponseDto;
import com.aaa.entity.Staff;
import com.aaa.service.StaffService;
import com.aaa.service.impl.StaffServiceImpl;
import com.google.gson.Gson;
import com.sun.org.apache.regexp.internal.RE;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateStaffServlet")
public class UpdateStaffServlet extends HttpServlet {

    private StaffService staffService;

    @Override
    public void init() throws ServletException {
        staffService = new StaffServiceImpl();
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        Staff staff = new Staff();
        ResponseDto responseDto = new ResponseDto();
        try {
            String staffId = request.getParameter("staffId");
            if (staffId != null && !staffId.equals("")) {
                staff.setStaffId(Integer.valueOf(staffId));
            }

            String staffName = request.getParameter("staffName");
            staff.setStaffName(staffName);

            String phone = request.getParameter("phone");
            staff.setPhone(phone);

            String idCard = request.getParameter("idCard");
            staff.setIdCard(idCard);

            String address = request.getParameter("address");
            staff.setAddress(address);

            String status = request.getParameter("status");
            if (status != null && !status.equals("")) {
                staff.setStatus(Integer.valueOf(status));
            }

            String roleId = request.getParameter("roleId");
            if (roleId != null && !roleId.equals("")) {
                staff.setRoleId(Integer.valueOf(roleId));
            }

            String momo = request.getParameter("remake");
            staff.setMomo(momo);


            int result = staffService.updateStaffByStaffId(staff);
            if (result > 0) {
                responseDto.setMessage("更新员工成功");
                responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            } else {
                response.getWriter().write("更新员工失败");
                responseDto.setStatus(ResponseDto.FAILURE_CODE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage(e.getMessage());
            responseDto.setMessage("请求失败");
        }
        response.getWriter().write(new Gson().toJson(responseDto));
    }
}
